package com.hxzy.jsp.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.hxzy.jsp.connection.JNDIBBSConection;

public abstract class BaseDAO {
	protected Connection conn;

	public Connection open() {
		/*File file=new File(this.getClass().getResource("app.xml").getPath());*/
		/*File file=new File("F:/Java/jsp/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/jsp-chapter03-interactive/WEB-INF/classes/app.xml");
		SAXReader saxReader=new SAXReader();
		Document document=null;
		try {
			document=saxReader.read(file);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Element element = document.getRootElement();
		String bbsConection = element.element("connection").element("connection-class").getTextTrim();
		Class<?> clazz = null;
		BBSConection bbsConection2 = null;
		try {
			clazz = Class.forName(bbsConection);
			bbsConection2 = (BBSConection) clazz.newInstance();
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}

		this.conn = bbsConection2.get();*/
		JNDIBBSConection jndibbsConection = new JNDIBBSConection();
		this.conn = jndibbsConection.get();
		return this.conn;
	}

	public void startTransaction() {
		try {
			this.conn.setAutoCommit(false);
		} catch (SQLException e) {
			throw new RuntimeException("关闭隐式事务提交失败：".concat(e.getMessage()));
		}
	}

	public void commitTransaction() {
		try {
			this.conn.commit();
		} catch (SQLException e) {
			throw new RuntimeException("事务提交失败：".concat(e.getMessage()));
		}
	}

	public void rollbackTransaction() {
		try {
			this.conn.rollback();
		} catch (SQLException e) {
			throw new RuntimeException("事务提交失败：".concat(e.getMessage()));
		}
	}
	
	public void close() {
		try {
			if (this.conn == null) {
				return;
			}
			conn.close();
		} catch (SQLException e) {
			throw new RuntimeException("关闭连接失败：".concat(e.getMessage()));
		}
	}

	/**
	 * 提供 PrepareStatement.excuteUpdate() 方法的快捷操作
	 * 
	 * @param preparedSQL
	 *            预编译的 sql 语句
	 * @param params
	 *            预编译 sql 的参数
	 * @return 0 表示没有更新到任何数据，>0 表示被该条更新语句影响的记录条数
	 */
	public int executeUpdate(final String preparedSQL, final Object[] params) {
		open();
		try {
			startTransaction();
			PreparedStatement stmt = this.conn.prepareStatement(preparedSQL);
			if (params != null) {
				for (int i = 1; i <= params.length; i++) {
					Object object = params[i - 1];
					if (object instanceof String) {
						stmt.setString(i, String.valueOf(object));
					} else if (object instanceof Integer) {
						stmt.setInt(i, Integer.parseInt(String.valueOf(object))); // 将字符串转换成
																					// int
																					// 值，强制类型转换
					} else {
						stmt.setObject(i, object);
					}
				}
			}
			int rows = stmt.executeUpdate();
			commitTransaction();
			return rows;
		} catch (SQLException e) {
			rollbackTransaction();
		} finally {
			close();
		}
		return 0;
	}


}
